<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              class="login_btn"
              type="primary"
              size="default"
              @click="login"
              :loading="loading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
//引入用户相关小仓库
import useUserStore from '@/store/modules/user'
let useStore = useUserStore()

import { useRouter, useRoute } from 'vue-router'
let $router = useRouter()
let $route = useRoute()
import { ElNotification } from 'element-plus'

//引入获取当前时间的函数
import { getTime } from '@/utils/time'

let loginForms = ref()

//收集帐号与密码的数据
let loginForm = reactive({ username: 'admin', password: '111111' })

//控制按钮加载
let loading = ref(false)

const login = async () => {
  //保证全部表单项校验通过
  await loginForms.value.validate()

  console.log('ddd')
  loading.value = true
  // console.log(loginForm)
  //点击登录后做什么
  //1通知仓库发登录请求
  //请求成功-》首页展示数据
  //请求失败-》弹出登录失败信息
  try {
    //保证登录成功
    await useStore.userLogin(loginForm)

    //判断是否有query，没有跳首页
    let redirect: any = $route.query.redirect
    $router.push({ path: redirect || '/' })

    //登录成功
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `HI，${getTime()}好`,
    })
    loading.value = false
  } catch (error) {
    //登录失败
    loading.value = false

    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  }
}

//rule 校验规则对象
//value 表单元素文本
//callback 符合条件放行，不符合则注入错误信息
const validatorUserName = (rule: any, value: any, callback: any) => {
  console.log('rule', rule)
  // if(/^\d{5,10}$/.test(value)){
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('帐号长度至少5位'))
  }
}

const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码长度至少是6位'))
  }
}

//定义表单校验对象
const rules = {
  username: [
    //规则对象属性
    //required字段必须校验
    //min文本长度至少
    //max文本长度最多
    //message错误信息
    //tringger 触发校验时机，change文本发生变化触发，blur失去焦点触发
    // { required: true, message: '用户名不能为空', trigger: 'blur' },
    // {
    //   required: true,
    //   min: 5,
    //   max: 10,
    //   message: '帐号长度至少5位',
    //   trigger: 'change',
    // },
    { trigger: 'change', validator: validatorUserName },
  ],
  password: [
    // {
    //   required: true,
    //   min: 6,
    //   max: 15,
    //   message: '密码的长度至少6位',
    //   trigger: 'change',
    // },
    { trigger: 'change', validator: validatorPassword },
  ],
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
